package com.cms.recursoshumanos.facade;

import java.io.UnsupportedEncodingException;

import net.rim.device.api.ui.UiApplication;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.facade.BaseFacade;
import com.cms.recursoshumanos.handler.BaseHandler;
import com.cms.recursoshumanos.handler.JParserHandler;
import com.cms.recursoshumanos.handler.MyUserDetailScreenHandler;
import com.cms.recursoshumanos.handler.WServiceCall;
import com.cms.recursoshumanos.json.object.JsonObjectAuthenticateUser;
import com.cms.recursoshumanos.object.Usuario;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.transport.BBRequest;
import com.cms.recursoshumanos.transport.BBRequestQueue;
import com.cms.recursoshumanos.transport.HttpRequest;

public class LoginScreenFacade extends BaseFacade implements BBRequest.Listener
{
	private String strEmail;
	private String strPass;
	
	private ShowDialogAlert alerta;
	
	private HttpRequest req;
	
	public LoginScreenFacade(String user,String password)
	{
		this.strEmail=user;
		this.strPass=password;
	}
	
	public void getLoginSistema()
	{
		enviarSolicitud(WServiceCall.getInstance().auntenticar_usuario(strEmail, strPass));
	}
	
	public void enviarSolicitud(String envelope_soap) 
	{
		try 
		{
			req = new HttpRequest();
			req.setRequestURL(Constant.URL_WEBSERVICE);	
			req.setPayload(envelope_soap, "UTF-8");
			req.setListener(this);
	        BBRequestQueue.getInstance().addRequest(req);
		} 
		catch (UnsupportedEncodingException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
  
	}
	
	public void requestStarted(BBRequest request) 
	{
		synchronized (UiApplication.getEventLock()) 
		{
			ScreenEngine.getInstance().showLoadingPopup("Cargando");
		}
		
	}
	
	public void requestCancelled(BBRequest request) 
	{
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("Se cayo la conexion inesperadamente, intente nuevamente");
		alerta.run();
	}

	public void requestFailed(BBRequest request)
	{
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("No se pudo conectar al servidor, intete nuevamente");
		alerta.run();
	}


	public void requestSucceeded(BBRequest request) 
	{
		String result = JParserHandler.getInstance().procesarXML(request);
		ScreenEngine.getInstance().hideLoadingPopup();
		this.setValores(result);
	}


	public void setValores(String strResult)
	{
			
		if (strResult.equals(null))
		{
			ShowDialogAlert alerta = new ShowDialogAlert("No se pudo obtener informacion del servidor, chequee su conexion");
			alerta.run();
		}
		else
		{
			JsonObjectAuthenticateUser.getInstance().fromJSON(strResult);
			if(!JsonObjectAuthenticateUser.getInstance().getResult().equals(Constant.CODE_OK)) 
			{
				ShowDialogAlert alerta = new ShowDialogAlert("No se pudo autentifcar el usuario");
				alerta.run();
			} 
			else 
			{	
				
				/*if(!UserInfoCache.getInstance().hasInfoUser())
				{*/
					UserInfoCache.getInstance().setUsuario(new Usuario(0, strEmail, strPass));
					MyUserDetailScreenHandler handler = new MyUserDetailScreenHandler();
					handler.getLoginUserinfo(strEmail,strPass);
				/*}
				else
				{
					UserInfoCache.getInstance().cargar_info_persistent();
					BaseHandler.getInstance().goPantallaPrincipal();
				}*/
			}
		}
		
	}

	
}
